<!DOCTYPE html>
<html class="x-admin-sm">

<head>
    <meta charset="UTF-8">
    <title>添加用户</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8"/>
    <link rel="stylesheet" href="../../css/font.css">
    <link rel="stylesheet" href="../../css/xadmin.css">
    <script type="text/javascript" src="../../lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="../../js/xadmin.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>
<div class="layui-fluid">
    <div class="layui-row">
        <form class="layui-form" lay-filter="">
            <div class="layui-form-item">
                <strong style="font-size: 20px">添加用户</strong>
            </div>
            <div class="layui-form-item">

                <label class="layui-form-label">
                    登录名
                </label>
                <div class="layui-input-inline">
                    <input type="text" id="uLoginName" lay-verify="uLoginName" placeholder="请输入登录名" autocomplete="off"
                           class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">密码</label>
                <div class="layui-input-inline">
                    <input type="password" id="uPassword" required lay-verify="pass" placeholder="请输入密码"
                           autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux">请填写6到12位密码</div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">所属科室</label>
                <div class="layui-input-inline">
                    <select id="officesId" lay-verify="required" lay-search="">
                        <option value=""></option>
                        <option value="1">急诊科</option>
                        <option value="2">儿科</option>
                        <option value="3">五官科</option>
                        <option value="4">内科</option>
                        <option value="5">外科</option>
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">所属部门</label>
                <div class="layui-input-inline">
                    <select id="departId" lay-verify="required" lay-search="">
                        <option value=""></option>
                        <option value="20">门诊部</option>
                        <option value="21">行政部</option>
                        <option value="22">库房部</option>
                        <option value="23">药房部</option>
                        <option value="24">财务部</option>
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">用户入职时间</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input test-item" id="uEntryDate" placeholder="用户入职日期">
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">用户上班时间</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input test-item1" id="uStartDate" placeholder="上班时间">
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">用户下班时间</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input test-item1" id="uStopDate" placeholder="下班时间">
                    </div>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">真实姓名</label>
                <div class="layui-input-inline">
                    <input type="text" id="uName" lay-verify="required" placeholder="请输入真实姓名" autocomplete="off"
                           class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label"> 性别</label>
                <div class="layui-input-inline">
                    <select id="uGender" lay-verify="required" lay-search="">
                        <option value=""></option>
                        <option value="男">男</option>
                        <option value="女">女</option>
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">身份证号码</label>
                <div class="layui-input-inline">
                    <input type="text" id="uCardId" lay-verify="identity" placeholder="请输入身份证号码" autocomplete="off"
                           class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">手机号码</label>
                <div class="layui-input-inline">
                    <input type="text" id="uPhone" lay-verify="uPhone" placeholder="" autocomplete="off"
                           class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">用户住址</label>
                <div class="layui-input-inline">
                    <input type="text" id="uAddress" lay-verify="required" placeholder="请输入用户住址" autocomplete="off"
                           class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">用户出生日期</label>
                    <div class="layui-input-inline">
                        <input type="text" class="layui-input test-item" id="uBirthday" placeholder="用户出生日期">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">用户年龄</label>
                <div class="layui-input-inline">
                    <input type="text" id="uAge" lay-verify="required" placeholder="请输入年龄" autocomplete="off"
                           class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">用户角色</label>
                <div class="layui-input-block" id="radioDiv">
                    <!--<input type="radio" value="1" name="user_type" title="前台" lay-filter="ChoiceRadio">
                    <input type="radio" value="2" name="user_type" title="医生" lay-filter="ChoiceRadio">
                    <input type="radio" value="3" name="user_type" title="收银" lay-filter="ChoiceRadio">
                    <input type="radio" value="4" name="user_type" title="药房管理" lay-filter="ChoiceRadio">
                    <input type="radio" value="5" name="user_type" title="库房管理" lay-filter="ChoiceRadio">
                    <input type="radio" value="6" name="user_type" title="超级管理员" lay-filter="ChoiceRadio">-->
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label"></label>
                <button type="button" class="layui-btn" lay-filter="add" lay-submit="">增加</button>
            </div>

        </form>

    </div>
</div>
<script>
    layui.use(['form', 'layer', 'jquery', 'laydate'], function () {
        var form = layui.form,
            laydate = layui.laydate,
            layer = layui.layer,
            $ = layui.jquery;

        //常规用法

        lay('.test-item1').each(function () {
            //时间选择器
            laydate.render({
                elem: this
                , format: 'HH:mm:ss'
                , type: 'time'
                , trigger: 'click'
            });
        });

        lay('.test-item').each(function () {
            //时间选择器
            laydate.render({
                elem: this
                , format: 'yyyy-MM-dd'
                , type: 'date'
                , trigger: 'click'
            });
        });
        var phone = /^0\d{2,3}-?\d{7,8}$/;
        form.verify({
            uLoginName: function (value, item) { //value：表单的值、item：表单的DOM对象
                if (!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)) {
                    return '用户名不能有特殊字符';
                }
                if (/(^\_)|(\__)|(\_+$)/.test(value)) {
                    return '用户名首尾不能出现下划线\'_\'';
                }
                if (/^\d+\d+\d$/.test(value)) {
                    return '用户名不能全为数字';
                }
            }
            , pass: [
                /^[\S]{6,12}$/
                , '密码必须6到12位，且不能出现空格'
            ]

            , identity: [/(^\d{15}$)|(^\d{17}(x|X|\d)$)/, "请输入正确的身份证号"]
        });

        getClassify();

        //监听提交
        form.on('submit(add)',
            function () {
                var uLoginName1 = $('#uLoginName').val(),
                    uPassword = $('#uPassword').val(),
                    officesId = $('#officesId').val(),
                    departId = $('#departId').val(),
                    uEntryDate = $('#uEntryDate').val(),
                    uStartDate = $('#uStartDate').val(),
                    uStopDate = $('#uStopDate').val(),
                    uName = $('#uName').val(),
                    uAge = $('#uAge').val(),
                    uCardId = $('#uCardId').val(),
                    uPhone = $('#uPhone').val(),
                    uAddress = $('#uAddress').val(),
                    uBirthday = $('#uBirthday').val(),
                    uGender = $('#uGender').val(),
                    tId = $('input[name="user_type"]:checked').val();
                console.log(tId)
                $.ajax({
                    url: '/JT90/user/selectUserByuLoginName1',//地址
                    dataType: 'json',//数据类型
                    data: {
                        "uLoginName": uLoginName1,
                    },
                    type: 'GET',//类型
                    contentType: 'application/json;charset=UTF-8',
                    //请求成功
                    success: function (map) {
                        if (map.msg) {
                                $.ajax({
                                    url: '/JT90/user/insertUser',//地址
                                    dataType: 'json',//数据类型
                                    data: {
                                        "uLoginName": uLoginName1,
                                        "uPassword": uPassword,
                                        "officesId": officesId,
                                        "departId": departId,
                                        "uEntryDate": uEntryDate,
                                        "uStartDate": uStartDate,
                                        "uStopDate": uStopDate,
                                        "uName": uName,
                                        "uAge": uAge,
                                        "uCardId": uCardId,
                                        "uPhone": uPhone,
                                        "uAddress": uAddress,
                                        "uBirthday": uBirthday,
                                        "tId": tId,
                                        "uGender": uGender
                                    },
                                    type: 'GET',//类型
                                    contentType: 'application/json;charset=UTF-8',
                                    //请求成功
                                    success: function (map) {
                                        if (map.msg) {

                                            layer.alert("增加成功", {
                                                    icon: 6
                                                },
                                                function () {
                                                    //关闭当前frame
                                                    xadmin.close();

                                                    // 可以对父窗口进行刷新
                                                    xadmin.father_reload();
                                                });

                                        }
                                        return false;
                                    },
                                    //失败/超时
                                    error: function () {
                                        alert('請求超時');
                                        //alert(errorThrown);
                                    }
                                });
                            } else {
                                layer.alert("该用户名已存在！请重新输入", {
                                        icon: 6
                                    },
                                    function () {
                                        //关闭当前frame
                                        xadmin.close();
                                    });
                            }
                    },
                    //失败/超时
                    error: function () {
                        alert('請求超時');
                        //alert(errorThrown);
                    }
                });


                //发异步，把数据提交给php

            });


        function getClassify() {
            $.ajax({
                url: '/JT90/user_type/selectUserTypeList',//地址
                dataType: 'json',//数据类型
                type: 'GET',//类型
                contentType: 'application/json;charset=UTF-8',
                //请求成功
                success: function (map) {
                    if (map.msg) {
                        for (var i = 0; i < map.data.length; i++) {
                            if (map.data[i].roleState == 15) {
                                $("#radioDiv").append(" <input type=radio value=" + map.data[i].roleId + " name=user_type title=" + map.data[i].roleName + " lay-filter=ChoiceRadio>");
                            } else {
                                $("#radioDiv").append(" <input type=radio value=" + map.data[i].roleId + " name=user_type title=" + map.data[i].roleName + " lay-filter=ChoiceRadio disabled>");
                            }
                        }
                        //重新渲染
                        layui.form.render("radio");
                    }
                },
            });

        }
    });

</script>
</body>

</html>